<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace app\admin\controller;


use addon\supermember\model\MemberLevelOrder;
use app\model\shop\ShopApply;
use app\model\system\Stat as StatModel;
use app\model\system\User as UserModel;
use Carbon\Carbon;
use app\model\order\Order as OrderModel;

/**
 * 统计管理 控制器
 */
class Stat extends BaseAdmin
{
	/**
	 * 统计概况
	 */
	public function index()
	{
        if (request()->isAjax()) {
            $date_type = input('date_type', 0);

            if($date_type == 0){
                $start_time = strtotime("today");
                $time_range = date('Y-m-d',$start_time);
            }else if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }

            $stat_model = new StatModel();

            $shop_stat_sum = $stat_model->getShopStatSum(0, $start_time);

            $shop_stat_sum['data']['time_range'] = $time_range;

            return $shop_stat_sum;
        }else{
            return $this->fetch("stat/index");
        }
	}

    /**
     * 店铺统计报表
     * */
    public function getStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 6;
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 29;
            }

            $stat_model = new StatModel();

            $stat_list = $stat_model->getShopStatList(0, $start_time);

            //将时间戳作为列表的主键
            $shop_stat_list = array_column($stat_list['data'], null, 'day_time');

            $data = array();

            for ($i = 0;$i <= $day;$i++){
                $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
                $data['time'][$i] = date('Y-m-d',$time);
                if(array_key_exists($time, $shop_stat_list)){
                    $data['order_total'][$i] = $shop_stat_list[$time]['order_total'];
                    $data['shipping_total'][$i] = $shop_stat_list[$time]['shipping_total'];
                    $data['refund_total'][$i] = $shop_stat_list[$time]['refund_total'];
                    $data['order_pay_count'][$i] = $shop_stat_list[$time]['order_pay_count'];
                    $data['goods_pay_count'][$i] = $shop_stat_list[$time]['goods_pay_count'];
                    $data['shop_money'][$i] = $shop_stat_list[$time]['shop_money'];
                    $data['platform_money'][$i] = $shop_stat_list[$time]['platform_money'];
                    $data['collect_shop'][$i] = $shop_stat_list[$time]['collect_shop'];
                    $data['collect_goods'][$i] = $shop_stat_list[$time]['collect_goods'];
                    $data['visit_count'][$i] = $shop_stat_list[$time]['visit_count'];
                    $data['order_count'][$i] = $shop_stat_list[$time]['order_count'];
                    $data['goods_count'][$i] = $shop_stat_list[$time]['goods_count'];
                    $data['add_goods_count'][$i] = $shop_stat_list[$time]['add_goods_count'];
                    $data['member_count'][$i] = $shop_stat_list[$time]['member_count'];
                }else{
                    $data['order_total'][$i] = 0.00;
                    $data['shipping_total'][$i] = 0.00;
                    $data['refund_total'][$i] = 0.00;
                    $data['order_pay_count'][$i] = 0;
                    $data['goods_pay_count'][$i] = 0;
                    $data['shop_money'][$i] = 0.00;
                    $data['platform_money'][$i] = 0.00;
                    $data['collect_shop'][$i] = 0;
                    $data['collect_goods'][$i] = 0;
                    $data['visit_count'][$i] = 0;
                    $data['order_count'][$i] = 0;
                    $data['goods_count'][$i] = 0;
                    $data['add_goods_count'][$i] = 0;
                    $data['member_count'][$i] = 0;
                }
            }

            $data['time_range'] = $time_range;

            return $data;
        }
    }

    /**
     * 交易分析
     */
    public function order()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 0);

            if($date_type == 0){
                $start_time = strtotime("today");
                $time_range = date('Y-m-d',$start_time);
            }else if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }

            $stat_model = new StatModel();

            $shop_stat_sum = $stat_model->getShopStatSum(0, $start_time);

            $shop_stat_sum['data']['time_range'] = $time_range;

            return $shop_stat_sum;
        }else{
            return $this->fetch("stat/order");
        }
    }

    /**
     * 交易统计报表
     * */
    public function getOrderStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 6;
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 29;
            }

            $stat_model = new StatModel();

            $stat_list = $stat_model->getShopStatList(0, $start_time);

            //将时间戳作为列表的主键
            $shop_stat_list = array_column($stat_list['data'], null, 'day_time');

            $data = array();

            for ($i = 0;$i <= $day;$i++){
                $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
                $data['time'][$i] = date('Y-m-d',$time);
                if(array_key_exists($time, $shop_stat_list)){
                    $data['order_total'][$i] = $shop_stat_list[$time]['order_total'];
                    $data['shipping_total'][$i] = $shop_stat_list[$time]['shipping_total'];
                    $data['refund_total'][$i] = $shop_stat_list[$time]['refund_total'];
                    $data['order_pay_count'][$i] = $shop_stat_list[$time]['order_pay_count'];
                    $data['goods_pay_count'][$i] = $shop_stat_list[$time]['goods_pay_count'];
                    $data['shop_money'][$i] = $shop_stat_list[$time]['shop_money'];
                    $data['platform_money'][$i] = $shop_stat_list[$time]['platform_money'];
                    $data['collect_shop'][$i] = $shop_stat_list[$time]['collect_shop'];
                    $data['collect_goods'][$i] = $shop_stat_list[$time]['collect_goods'];
                    $data['visit_count'][$i] = $shop_stat_list[$time]['visit_count'];
                    $data['order_count'][$i] = $shop_stat_list[$time]['order_count'];
                    $data['goods_count'][$i] = $shop_stat_list[$time]['goods_count'];
                    $data['add_goods_count'][$i] = $shop_stat_list[$time]['add_goods_count'];
                    $data['member_count'][$i] = $shop_stat_list[$time]['member_count'];
                }else{
                    $data['order_total'][$i] = 0.00;
                    $data['shipping_total'][$i] = 0.00;
                    $data['refund_total'][$i] = 0.00;
                    $data['order_pay_count'][$i] = 0;
                    $data['goods_pay_count'][$i] = 0;
                    $data['shop_money'][$i] = 0.00;
                    $data['platform_money'][$i] = 0.00;
                    $data['collect_shop'][$i] = 0;
                    $data['collect_goods'][$i] = 0;
                    $data['visit_count'][$i] = 0;
                    $data['order_count'][$i] = 0;
                    $data['goods_count'][$i] = 0;
                    $data['add_goods_count'][$i] = 0;
                    $data['member_count'][$i] = 0;
                }
            }

            $data['time_range'] = $time_range;

            return $data;
        }
    }

    /**
     * 商品统计
     * @return mixed
     */
    public function goods()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 0);

            if($date_type == 0){
                $start_time = strtotime("today");
                $time_range = date('Y-m-d',$start_time);
            }else if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }

            $stat_model = new StatModel();

            $shop_stat_sum = $stat_model->getShopStatSum(0, $start_time);

            $shop_stat_sum['data']['time_range'] = $time_range;

            return $shop_stat_sum;
        }else{
            return $this->fetch("stat/goods");
        }
    }

    /**
     * 商品统计报表
     * */
    public function getGoodsStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 6;
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 29;
            }

            $stat_model = new StatModel();

            $stat_list = $stat_model->getShopStatList(0, $start_time);

            //将时间戳作为列表的主键
            $shop_stat_list = array_column($stat_list['data'], null, 'day_time');

            $data = array();

            for ($i = 0;$i <= $day;$i++){
                $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
                $data['time'][$i] = date('Y-m-d',$time);
                if(array_key_exists($time, $shop_stat_list)){
                    $data['order_total'][$i] = $shop_stat_list[$time]['order_total'];
                    $data['shipping_total'][$i] = $shop_stat_list[$time]['shipping_total'];
                    $data['refund_total'][$i] = $shop_stat_list[$time]['refund_total'];
                    $data['order_pay_count'][$i] = $shop_stat_list[$time]['order_pay_count'];
                    $data['goods_pay_count'][$i] = $shop_stat_list[$time]['goods_pay_count'];
                    $data['shop_money'][$i] = $shop_stat_list[$time]['shop_money'];
                    $data['platform_money'][$i] = $shop_stat_list[$time]['platform_money'];
                    $data['collect_shop'][$i] = $shop_stat_list[$time]['collect_shop'];
                    $data['collect_goods'][$i] = $shop_stat_list[$time]['collect_goods'];
                    $data['visit_count'][$i] = $shop_stat_list[$time]['visit_count'];
                    $data['order_count'][$i] = $shop_stat_list[$time]['order_count'];
                    $data['goods_count'][$i] = $shop_stat_list[$time]['goods_count'];
                    $data['add_goods_count'][$i] = $shop_stat_list[$time]['add_goods_count'];
                    $data['member_count'][$i] = $shop_stat_list[$time]['member_count'];
                }else{
                    $data['order_total'][$i] = 0.00;
                    $data['shipping_total'][$i] = 0.00;
                    $data['refund_total'][$i] = 0.00;
                    $data['order_pay_count'][$i] = 0;
                    $data['goods_pay_count'][$i] = 0;
                    $data['shop_money'][$i] = 0.00;
                    $data['platform_money'][$i] = 0.00;
                    $data['collect_shop'][$i] = 0;
                    $data['collect_goods'][$i] = 0;
                    $data['visit_count'][$i] = 0;
                    $data['order_count'][$i] = 0;
                    $data['goods_count'][$i] = 0;
                    $data['add_goods_count'][$i] = 0;
                    $data['member_count'][$i] = 0;
                }
            }

            $data['time_range'] = $time_range;

            return $data;
        }
    }

    /**
     * 会员统计
     * @return mixed
     */
    public function member()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 0);

            if($date_type == 0){
                $start_time = strtotime("today");
                $time_range = date('Y-m-d',$start_time);
            }else if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }

            $stat_model = new StatModel();

            $shop_stat_sum = $stat_model->getShopStatSum(0, $start_time);

            $shop_stat_sum['data']['time_range'] = $time_range;

            return $shop_stat_sum;
        }else{
            return $this->fetch("stat/member");
        }
    }

    /**
     * 会员统计报表
     * */
    public function getMemberStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 6;
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 29;
            }

            $stat_model = new StatModel();

            $stat_list = $stat_model->getShopStatList(0, $start_time);

            //将时间戳作为列表的主键
            $shop_stat_list = array_column($stat_list['data'], null, 'day_time');

            $data = array();

            for ($i = 0;$i <= $day;$i++){
                $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
                $end_time = $time+86400;
                $data['time'][$i] = date('Y-m-d',$time);
                $member_count = model('member_level_order')->getCount([
                    ['pay_status','=',1],
                    [ 'pay_time', 'between', [ $time, $end_time ] ]
                ]);
                $data['member_count'][$i] = $member_count;
                /*if(array_key_exists($time, $shop_stat_list)){
                    $data['order_total'][$i] = $shop_stat_list[$time]['order_total'];
                    $data['shipping_total'][$i] = $shop_stat_list[$time]['shipping_total'];
                    $data['refund_total'][$i] = $shop_stat_list[$time]['refund_total'];
                    $data['order_pay_count'][$i] = $shop_stat_list[$time]['order_pay_count'];
                    $data['goods_pay_count'][$i] = $shop_stat_list[$time]['goods_pay_count'];
                    $data['shop_money'][$i] = $shop_stat_list[$time]['shop_money'];
                    $data['platform_money'][$i] = $shop_stat_list[$time]['platform_money'];
                    $data['collect_shop'][$i] = $shop_stat_list[$time]['collect_shop'];
                    $data['collect_goods'][$i] = $shop_stat_list[$time]['collect_goods'];
                    $data['visit_count'][$i] = $shop_stat_list[$time]['visit_count'];
                    $data['order_count'][$i] = $shop_stat_list[$time]['order_count'];
                    $data['goods_count'][$i] = $shop_stat_list[$time]['goods_count'];
                    $data['add_goods_count'][$i] = $shop_stat_list[$time]['add_goods_count'];
                    $data['member_count'][$i] = $shop_stat_list[$time]['member_count'];
                }else{
                    $data['order_total'][$i] = 0.00;
                    $data['shipping_total'][$i] = 0.00;
                    $data['refund_total'][$i] = 0.00;
                    $data['order_pay_count'][$i] = 0;
                    $data['goods_pay_count'][$i] = 0;
                    $data['shop_money'][$i] = 0.00;
                    $data['platform_money'][$i] = 0.00;
                    $data['collect_shop'][$i] = 0;
                    $data['collect_goods'][$i] = 0;
                    $data['visit_count'][$i] = 0;
                    $data['order_count'][$i] = 0;
                    $data['goods_count'][$i] = 0;
                    $data['add_goods_count'][$i] = 0;
                    $data['member_count'][$i] = 0;
                }*/
            }

            $data['time_range'] = $time_range;

            return $data;
        }
    }

    //会员相关统计
    public function memberaccount()
    {
        if (request()->isAjax()) {

        }else{
            $member_model = new \app\model\member\Member();
            $login_num_member_list = $member_model->getMemberList([],'member_id,login_num,mobile,nickname,member_card_type','login_num desc', 'a', $join = [], $group = '', $limit = 10);
            //$login_num_member_list = model('member')->getList([],'member_id,login_num,mobile,nickname,member_card_type','login_num desc');
            $point_member_list = $member_model->getMemberList([],'member_id,login_num,mobile,nickname,member_card_type,point','point desc', 'a', $join = [], $group = '', $limit = 10);
            $this->assign('login_num_member_list',$login_num_member_list['data']);
            $this->assign('point_member_list',$point_member_list['data']);

            $carbon = Carbon::now();

            $stat_model = new StatModel();

            $stat_info = $stat_model->getStatShop(0, $carbon->year, $carbon->month, $carbon->day);
            $this->assign('stat_info', $stat_info[ 'data' ]);

            $today_start_time = strtotime('today');
            //基础统计信息
            /*$today = Carbon::now();
            $yesterday = Carbon::yesterday();
            $stat_today = $stat_model->getStatShop(0, $today->year, $today->month, $today->day);
            $stat_yesterday = $stat_model->getStatShop(0, $yesterday->year, $yesterday->month, $yesterday->day);
            $this->assign("stat_day", $stat_today[ 'data' ]);
            $this->assign("stat_yesterday", $stat_yesterday[ 'data' ]);
            $this->assign("today", $today);*/

            //日同比
            //$day_rate[ 'member_count' ] = diff_rate($stat_today[ 'data' ][ 'member_count' ], $stat_yesterday[ 'data' ][ 'member_count' ]);

            $today_member_count = model('member_level_order')->getCount([
                ['pay_status','=',1],
                ['pay_time','>',$today_start_time]
            ]);

            $yesterday_member_count = model('member_level_order')->getCount([
                ['pay_status','=',1],
                [ 'pay_time', 'between', [ $today_start_time-86400, $today_start_time ] ]
            ]);
            $member_count = diff_rate($today_member_count,$yesterday_member_count);
            $this->assign('today_member_count', $today_member_count);
            $this->assign('yesterday_member_count', $yesterday_member_count);
            $this->assign('member_count', $member_count);
            //会员总数
            //$user_model = new UserModel();
            //$member_count = $user_model->getMemberTotalCount();
            $member_count = $member_model->getMemberCount([['member_level','>',1]]);
            $this->assign('total_member_count', $member_count[ 'data' ]);

            //消费金额
            $hotel_order_money = model('hotel_order')->getSum([['pay_status','=',1]],'order_money');
            $this->assign('hotel_order_money', $hotel_order_money);
            //充值

            //累计冲的金额
            $recharge_order_total = model('member_recharge_order')->getSum(['status'=>2],'face_value');
            $this->assign('recharge_order_total', $recharge_order_total);
            //今天冲的金额
            $today_recharge_order_total = model('member_recharge_order')->getSum([
                ['status','=',2],
                ['pay_time','>',$today_start_time]
            ],'face_value');
            $this->assign('today_recharge_order_total', $today_recharge_order_total);
            //昨天天冲的金额
            $yesterday_recharge_order_total = model('member_recharge_order')->getSum([
                ['status','=',2],
                [ 'pay_time', 'between', [ $today_start_time-86400, $today_start_time ] ]
            ],'face_value');
            $this->assign('yesterday_recharge_order_total', $yesterday_recharge_order_total);

            $day_rate[ 'recharge_order_total' ] = diff_rate($today_recharge_order_total, $yesterday_recharge_order_total);
            $this->assign('day_rate', $day_rate);

            //昨天使用余额
            $yesterday_money = model('member_account')->getSum([
                ['account_type','=','balance_money'],
                ['account_data','<',0],
                [ 'create_time', 'between', [ $today_start_time-86400, $today_start_time ] ]
            ],'account_data');
            $yesterday_money = abs($yesterday_money);
            $this->assign('yesterday_money', $yesterday_money);
            //今天使用余额
            $today_money = model('member_account')->getSum([
                ['account_type','=','balance_money'],
                ['account_data','<',0],
                [ 'create_time', '>', $today_start_time ]
            ],'account_data');
            $today_money = abs($today_money);
            $balance_money_total = diff_rate($today_money, $yesterday_money);
            $this->assign('today_money', $today_money);
            $this->assign('balance_money_total', $balance_money_total);
            return $this->fetch("stat/memberaccount");
        }
    }

    public function getMemberAnnularRingStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 0){
                $start_time = strtotime("today");
                $time_range = date('Y-m-d',$start_time);
            }else if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }
            $end_time = time();

            $member_model = new \app\model\member\Member();
            $member_stat_count = $member_model->getMemberStatCount($start_time,$end_time);
            $data['member_count'] = $member_stat_count;
            $member_stat_balance = $member_model->getMemberBalanceMoneyProportion($start_time,$end_time);
            $data['member_balance'] = $member_stat_balance;
            $member_stat_point = $member_model->getMemberPointProportion($start_time,$end_time);
            $data['member_point'] = $member_stat_point;
            $data['time_range'] = $time_range;

            return $data;
        }
    }

    /**
     * 会员消费余额走势
     * */
    public function getMemberTypeConsumeBalanceStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

           if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 6;
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 29;
            }

            //普通用户id
            $ordinary_condition = [
                ['member_card_type','=',0],
            ];
            $ordinary_member_id = model('member')->getColumn($ordinary_condition,'member_id');

            //个人会员id
            $personal_condition = [
                ['member_card_type','=',1],
            ];
            $personal_member_id= model('member')->getColumn($personal_condition,'member_id');

            //门店会员id
            $store_condition = [
                ['member_card_type','=',2],
            ];
            $store_member_id = model('member')->getColumn($store_condition,'member_id');

            //员工会员户id
            $staff_condition = [
                ['member_card_type','=',3],
            ];
            $staff_member_id = model('member')->getColumn($staff_condition,'member_id');

            $data = array();

            for ($i = 0;$i <= $day;$i++){
                $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
                $end_time = $time+86400;
                $data['time'][$i] = date('Y-m-d',$time);

                $ordinary_balance_condition = [
                    ['from_type','=','order'],
                    ['member_id','in',$ordinary_member_id],
                    ['account_type','=','balance_money'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $ordinary_balance = model('member_account')->getSum($ordinary_balance_condition,'account_data');
                $data['ordinary_balance'][$i] = abs($ordinary_balance);

                $personal_balance_condition = [
                    ['from_type','=','order'],
                    ['member_id','in',$personal_member_id],
                    ['account_type','=','balance_money'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $personal_balance = model('member_account')->getSum($personal_balance_condition,'account_data');
                $data['personal_balance'][$i] = abs($personal_balance);

                $store_balance_condition = [
                    ['from_type','=','order'],
                    ['member_id','in',$store_member_id],
                    ['account_type','=','balance_money'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $store_balance = model('member_account')->getSum($store_balance_condition,'account_data');
                $data['store_balance'][$i] = abs($store_balance);

                $staff_balance_condition = [
                    ['from_type','=','order'],
                    ['member_id','in',$staff_member_id],
                    ['account_type','=','balance_money'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $staff_balance = model('member_account')->getSum($staff_balance_condition,'account_data');
                $data['staff_balance'][$i] = abs($staff_balance);
            }

            $data['time_range'] = $time_range;

            return $data;
        }
    }

    /**
     * 会员消费积分走势
     * */
    public function getMemberTypeConsumePointStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 6;
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 29;
            }

            //普通用户id
            $ordinary_condition = [
                ['member_card_type','=',0],
            ];
            $ordinary_member_id = model('member')->getColumn($ordinary_condition,'member_id');

            //个人会员id
            $personal_condition = [
                ['member_card_type','=',1],
            ];
            $personal_member_id= model('member')->getColumn($personal_condition,'member_id');

            //门店会员id
            $store_condition = [
                ['member_card_type','=',2],
            ];
            $store_member_id = model('member')->getColumn($store_condition,'member_id');

            //员工会员户id
            $staff_condition = [
                ['member_card_type','=',3],
            ];
            $staff_member_id = model('member')->getColumn($staff_condition,'member_id');

            $data = array();

            for ($i = 0;$i <= $day;$i++){
                $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
                $end_time = $time+86400;
                $data['time'][$i] = date('Y-m-d',$time);

                $ordinary_balance_condition = [
                    ['from_type','=','pointcash'],
                    ['member_id','in',$ordinary_member_id],
                    ['account_type','=','point'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $ordinary_balance = model('member_account')->getSum($ordinary_balance_condition,'account_data');
                $data['ordinary_point'][$i] = abs($ordinary_balance);

                $personal_balance_condition = [
                    ['from_type','=','pointcash'],
                    ['member_id','in',$personal_member_id],
                    ['account_type','=','point'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $personal_balance = model('member_account')->getSum($personal_balance_condition,'account_data');
                $data['personal_point'][$i] = abs($personal_balance);

                $store_balance_condition = [
                    ['from_type','=','pointcash'],
                    ['member_id','in',$store_member_id],
                    ['account_type','=','point'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $store_balance = model('member_account')->getSum($store_balance_condition,'account_data');
                $data['store_point'][$i] = abs($store_balance);

                $staff_balance_condition = [
                    ['from_type','=','pointcash'],
                    ['member_id','in',$staff_member_id],
                    ['account_type','=','point'],
                    [ 'create_time', 'between', [ $time, $end_time ] ]
                ];
                $staff_balance = model('member_account')->getSum($staff_balance_condition,'account_data');
                $data['staff_point'][$i] = abs($staff_balance);
            }

            $data['time_range'] = $time_range;

            return $data;
        }
    }

    /**
     * 会员类型
     * */
    public function membertype()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 6;
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
                $day = 29;
            }

            $data = array();

            for ($i = 0;$i <= $day;$i++){
                $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
                $end_time = $time+86400;
                $data['time'][$i] = date('Y-m-d',$time);

                $person_condition = [
                    ['member_card_type','=',1],
                    [ 'pay_time', 'between', [ $time, $end_time ] ]
                ];
                $person_count = model('member_level_order')->getCount($person_condition);
                $data['person_count'][$i] = $person_count;

                $store_condition = [
                    ['member_card_type','=',2],
                    [ 'pay_time', 'between', [ $time, $end_time ] ]
                ];
                $store_count = model('member_level_order')->getCount($store_condition);
                $data['store_count'][$i] = $store_count;
            }

            $data['time_range'] = $time_range;

            return $data;
        }
    }

    public function capital()
    {
        //充值
        $today_start_time = strtotime('today');
        //今天冲的金额
        $today_recharge_order_total = model('member_recharge_order')->getSum([
            ['status','=',2],
            ['pay_time','>',$today_start_time]
        ],'face_value');
        $this->assign('today_recharge_order_total', $today_recharge_order_total);
        //昨天天冲的金额
        $yesterday_recharge_order_total = model('member_recharge_order')->getSum([
            ['status','=',2],
            [ 'pay_time', 'between', [ $today_start_time-86400, $today_start_time ] ]
        ],'face_value');
        $this->assign('yesterday_recharge_order_total', $yesterday_recharge_order_total);

        $recharge_order_total = diff_rate($today_recharge_order_total, $yesterday_recharge_order_total);
        $this->assign('recharge_order_total', $recharge_order_total);

        //昨天使用余额
        $yesterday_money = model('member_account')->getSum([
            ['account_type','=','balance_money'],
            ['account_data','<',0],
            [ 'create_time', 'between', [ $today_start_time-86400, $today_start_time ] ]
        ],'account_data');
        $yesterday_money = abs($yesterday_money);
        $this->assign('yesterday_money', $yesterday_money);
        //今天使用余额
        $today_money = model('member_account')->getSum([
            ['account_type','=','balance_money'],
            ['account_data','<',0],
            [ 'create_time', '>', $today_start_time ]
        ],'account_data');
        $today_money = abs($today_money);
        $balance_money_total = diff_rate($today_money, $yesterday_money);
        $this->assign('today_money', $today_money);
        $this->assign('balance_money_total', $balance_money_total);

        //提现
        $today_withdraw_condition = [
            [ 'apply_time', '>', $today_start_time ]
        ];
        $today_withdraw = model('member_withdraw')->getSum($today_withdraw_condition,'apply_money');
        $this->assign('today_withdraw', $today_withdraw);
        $yesterday_withdraw_condition = [
            [ 'apply_time',  'between', [ $today_start_time-86400, $today_start_time ]  ]
        ];
        $yesterday_withdraw = model('member_withdraw')->getSum($yesterday_withdraw_condition,'apply_money');
        $this->assign('yesterday_withdraw', $yesterday_withdraw);

        $withdraw_total = diff_rate($today_withdraw, $yesterday_withdraw);
        $this->assign('withdraw_total', $withdraw_total);
        //积分获得
        $today_acquire_condition = [
            ['create_time', '>', $today_start_time ],
            ['account_type','=','point'],
            ['account_data','>',0],
        ];
        $today_member_point_acquire = model('member_account')->getSum($today_acquire_condition,'account_data');
        $this->assign('today_member_point_acquire', $today_member_point_acquire);

        $yesterday_acquire_condition = [
            ['create_time','between', [ $today_start_time-86400, $today_start_time ] ],
            ['account_type','=','point'],
            ['account_data','>',0],
        ];
        $yesterday_member_point_acquire = model('member_account')->getSum($yesterday_acquire_condition,'account_data');
        $this->assign('yesterday_member_point_acquire', $yesterday_member_point_acquire);

        $member_point_acquire_total = diff_rate($today_member_point_acquire, $yesterday_member_point_acquire);
        $this->assign('member_point_acquire_total', $member_point_acquire_total);
        //积分消费
        $today_consume_condition = [
            ['create_time', '>', $today_start_time ],
            ['account_type','=','point'],
            ['account_data','<',0],
        ];
        $today_member_point_consume = model('member_account')->getSum($today_consume_condition,'account_data');
        $this->assign('today_member_point_consume', $today_member_point_consume);

        $yesterday_consume_condition = [
            ['create_time','between', [ $today_start_time-86400, $today_start_time ] ],
            ['account_type','=','point'],
            ['account_data','<',0],
        ];
        $yesterday_member_point_consume = model('member_account')->getSum($yesterday_consume_condition,'account_data');
        $this->assign('yesterday_member_point_consume', $yesterday_member_point_consume);

        $member_point_consume_total = diff_rate($today_member_point_consume, $yesterday_member_point_consume);
        $this->assign('member_point_consume_total', $member_point_consume_total);
        return $this->fetch("stat/capital");
    }


    public function getMemberCapitalStatList()
    {
        if (request()->isAjax()) {
            $date_type = input('date_type', 1);

            if($date_type == 0){
                $start_time = strtotime("today");
                $time_range = date('Y-m-d',$start_time);
            }else if($date_type == 1){
                $start_time = strtotime(date('Y-m-d', strtotime("-6 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }else if($date_type == 2){
                $start_time = strtotime(date('Y-m-d', strtotime("-29 day")));
                $time_range = date('Y-m-d',$start_time).' 至 '.date('Y-m-d',strtotime("today"));
            }
            $end_time = time();

            $member_model = new \app\model\member\Member();


            $member_stat_recharge = $member_model->getMemberRecharge($start_time,$end_time);
            $data['member_recharge'] = $member_stat_recharge;

            $member_stat_consume = $member_model->getMemberConsume($start_time,$end_time);
            $data['member_consume'] = $member_stat_consume;

            $member_stat_withdraw = $member_model->getMemberWithdraw($start_time,$end_time);
            $data['member_withdraw'] = $member_stat_withdraw;
            $data['time_range'] = $time_range;

            return $data;
        }
    }
}